
#include "AbstractMessageQueue.h"

AbstractMessageQueue::AbstractMessageQueue() {
	
}

AbstractMessageQueue::~AbstractMessageQueue() 
{	
}

int AbstractMessageQueue::create(char *fileName, int id) 
{
	return this->getId(fileName, id, 0666|IPC_CREAT|IPC_EXCL);
}

int AbstractMessageQueue::getMessageQueue(char *fileName, int id) 
{
	return this->getId(fileName, id, 0666);
}

void AbstractMessageQueue::destroy () 
{
	msgctl ( this->id,IPC_RMID,NULL );
}

int AbstractMessageQueue::getId(char *fileName, int id, int flags) 
{	
	char buffer[100];
			
	key_t clave = ftok (fileName, id);
	if ( clave == -1 ) {	
		sprintf(buffer, "Error en MessageQueue - ftok: %s\n", strerror(errno));
		write(fileno(stdout), buffer, strlen(buffer)); 
		return errno;
	}
	
	this->id = msgget ( clave ,flags );
	if ( this->id == -1 ) {
		sprintf(buffer, "Error en MessageQueue - msgget: %s\n", strerror(errno));
		write(fileno(stdout), buffer, strlen(buffer)); 
		return errno;
	}
	
	return this->id;
}
